<?php

class eventClass
{
	
	//new function that gets the event information, use eventhelper for reference
	/*
	 * retrieves details about an event
	 * takes the ID of the event
	 * returns an array with details
	 * $event = array(
	 * 		'USER_ID'	=> userID		
	 * 		'NAME'		=> name
	 * 		'DESCRIPTION' => description
	 * 		'LOCATION'	=> location
	 * 		'HOSTED_BY'	=> hostName		// could be empty, but who is sponsoring the event
	 * 		'LINK'		=> url			// if the event has a webpage
	 * 		'IMAGE_PATH' => url			// image for the thumnail
	 * 		'START_TIME' => time
	 * 		'END_TIME'	=> time
	 * 		'TAGS' => array(
	 * 			[tag_id] => description
	 * 		)
	 * )
	 * 
	 */
	
	static function getEventDetails($eventID, $test = false)
	{
		if($test)
			$DB = database::getTestDB();
		else
			$DB = database::getDB();
		$date = ourDate::getDateString();
		
		/*
		$sql = "select * from events where ID = $eventID";
		//echo $sql; die();
		$result = mysqli_query($DB, $sql);
		while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
		{
			$event = array('USER_ID' => $row['USER_ID'], 'NAME' => $row['NAME'], 'DESCRIPTION' => $row['DESCRIPTION'], 'LOCATION' => $row['LOCATION'], 'HOSTED_BY' => $row['HOSTED_BY'], 'LINK' => $row['LINK'], 'IMAGE_PATH' => $row['IMAGE_PATH'], 'START_TIME' => $row['START_TIME'], 'END_TIME' => $row['END_TIME']);
			
			$sqlTags = "select t.ID, t.DESCRIPTION from tags as t left join event_has_tag as et on (t.ID = et.TAG_ID) where et.EVENT_ID = $eventID";
			$resultTags = mysqli_query($DB, $sqlTags);
			while($row = mysqli_fetch_array($resultTags, MYSQLI_ASSOC))
				$event['TAGS'][$row['ID']] = $row['DESCRIPTION'];
		}
			
		mysqli_free_result($result);
		mysqli_close($DB);
		*/
		
		$pq = $DB->prepare("SELECT * FROM events WHERE id = ?");
		$pq->execute(array($eventID));
		$result = $pq->fetch();
		
		$pq = $DB->prepare("SELECT t.id, t.name FROM tags AS t LEFT JOIN event_has_tag AS et ON (t.id = et.tag_id) WHERE et.event_id = ?");
		$pq->execute(array($eventID));
		$resultTags = $pq->fetchAll();
		$result['tags'] = $resultTags;
		
		//echo '<pre>'.print_r($result, true).'</pre>';
		
		return $result;
	}
	
	/*
	 * this wil retreive the three events that are most similar to the event entered
	 * takes an evend id (should is sanitied) and a limit
	 * returns an array with three events
	 */
	static function getSimilarEvents($eventID, $limit = 3)
	{
		$thisEvent = eventClass::getEventDetails($eventID);
		$thisEventTags = $thisEvent['tags'];
		
		$simEventIDs = eventClass::findSimilarEvents($eventID, $thisEventTags);
		foreach($simEventIDs as $key => $ID)
			$simEvents[$ID] = eventClass::getEventDetails($ID);
			
		return $simEvents;
		
		//echo '<pre>'.print_r($thisEvent, true).'</pre>'; die();
	}
	static function findSimilarEvents($thisEventID, $thisEventTags)
	{
		$DB = database::getDB();
		$date = ourDate::getDateString();
		
		foreach($thisEventTags as $ID => $name)
			$tagIDs[] = $ID;
		
		/*
		$sql = "select et.EVENT_ID as c from events as e left join event_has_tag as et on (et.EVENT_ID = e.ID) where e.START_TIME >= '$date' and et.TAG_ID in (".implode(', ', $tagIDs).") and et.EVENT_ID != $thisEventID group by (et.EVENT_ID) order by count(et.EVENT_ID) desc, e.START_TIME asc limit 3";
		
		$simEventIDs = array();
		$result = mysqli_query($DB, $sql);
		while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
			$simEventIDs[] = $row['c'];
			
		mysqli_free_result($result);
		mysqli_close($DB);
		*/
		
		$sql =  "SELECT et.event_id AS c FROM events AS e ";
		$sql .= "	LEFT JOIN event_has_tag AS et on (et.EVENT_ID = e.ID) ";
		$sql .= "WHERE e.start_time >= ? AND et.tag_id IN (?) AND et.event_id != ? ";
		$sql .= "GROUP BY (et.event_id) ORDER BY COUNT(et.event_id) DESC, e.start_time ASC LIMIT 3";
		$pq = $DB->prepare($sql);
		$pq->execute(array($date, implode(', ', $tagIDs), $thisEventID));
		$result = $pq->fetchAll();

		if(true || count($result) <= 2)
		{
			$sql = "SELECT id  FROM events WHERE start_time >= ? AND active = 1 ORDER BY start_time LIMIT ?";
			$pq = $DB->prepare($sql);
			$pq->execute(array($date, 3));
			$result = $pq->fetchAll();
		}
		
		foreach($result as $key => $res)
			$ids[] = $res['id'];
		
		//echo '<pre>'.print_r($ids, true).'</pre>'; die();
		
		return $ids;
	}
	
	
	
	/*
	 * use this function to creat a new event
	 * it takes everything an event needs, all inputs should be sanatied before
	 */
	static function createNewEvent($userID, $name, $description, $location, $host, $link, $image, $start, $end)
	{
		$sql = "insert into events (USER_ID, NAME, DESCRIPTION, LOCATION, HOSTED_BY, LINK, IMAGE_PATH, START_TIME, END_TIME) values ($userID, '$name', '$description', '$location', '$host', '$link', '$image', '$start', '$end')";
		//echo $sql; die();
		database::insertSQL($sql);
	}
	
	/*
	 * get the id of the event that we just created
	 * returns the id
	 */
	static function getEventIDByEverything($userID, $name, $description, $location, $host, $link, $image, $start, $end)
	{
		$DB = database::getDB();
		
		$sql = "select ID from events where USER_ID = $userID and NAME = '$name' and LOCATION = '$location' and HOSTED_BY = '$host'";
		//echo $sql; die();
		$result = mysqli_query($DB, $sql);
		while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
			$eventID = $row['ID'];
			
		mysqli_free_result($result);
		mysqli_close($DB);
		
		return $eventID;
	}
	
	/*
	 * this event adds the a tag to the event
	 * it takes the event id and tag id 
	 */
	static function addTagToNewEvent($eventID, $tagID)
	{
		$sql = "insert into event_has_tag values($eventID, $tagID)";
		//echo $sql; die();
		database::insertSQL($sql);
	}
}
?>